# Copyright 2004, Timothy Appnel, tim@appnel.com, Appnel Internet 
# Solutions. This code is released under the Artistic License.
#
# Packaged as 'MT::App::XSearch' by Hirotaka Ogawa.

package MT::App::XSearch;

use strict;
use MT::App;
@MT::App::XSearch::ISA = qw( MT::App );
use MT::ConfigMgr;
use MT::XSearch;

sub init {
    my $app = shift;
    $app->SUPER::init(@_) or return;
    $app->add_methods(main => \&view);
    $app->{charset} = $app->{cfg}->PublishCharset;
    $app->{default_mode} = 'main';
    $app;
}

sub view {
    my $app = shift;
    my $q = $app->{query};
    require MT::Template;
    require MT::Template::Context;
    my $blog_id = $q->param('blog_id') or
        return $app->error("Missing parameter blog_id");
    my $key = $q->param('search_key') or
        return $app->error("Missing parameter key");
    my $search = MT::XSearch->execute($q);
    my $tmpl = MT::Template->load({ blog_id => $blog_id, 
				    name => 'XSearch ' . $key,
				    type => 'custom' });
    my $ctx = MT::Template::Context->new;
    $ctx->stash('MT::XSearch', $search);
    $ctx->stash('CGI', $q);
    my $html = $tmpl->build($ctx)
        or return $app->error("Building search template failed: " . $tmpl->errstr);
    $html;
}

1;

__END__

=begin

=head1 NAME

mt-xsearch,cgi - A generic search script for use with XSearch framework plugins.

=head1 SYNOPSIS

<form method="get" action="<$MTCGIPath$>mt-xsearch.cgi">
<input type="hidden" name="blog_id" value="<$MTBlogID$>" />
<input type="hidden" name="search_key" value="grep" />
<label for="search" accesskey="4">Search this site:</label><br />
<input id="search" name="search" size="20" /><br />
<input type="submit" value="Search" />
</form>

=head1 DESCRIPTION 

Since MT template are associated to a specific weblog, the blog_id param 
is required. This issue is under review to make this script useful to 
doing multi-weblog and non-weblog searches.

Also, this script looks for a template module in the form "XSearch foo" where
foo is the key name of the plugin it is associated to. 

=head1 LICENSE

This code is released under the Artistic License. The terms of the 
Artistic License are described at L<http://www.perl.com/language/misc/Artistic.html>.

=head1 AUTHOR & COPYRIGHT

Except where otherwise noted, MT-XSearch is Copyright 2004, Timothy 
Appnel, Appnel Internet Solutions, LLC, (tim@appnel.com). 

Appnel Internet Solutions is a technology consultancy specializing 
in Movable Type and TypePad systems. For more information about how 
we can help you email info@appnel.com

=cut

=end
